package com.example.com.example.imooc.AsynTask;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.examplee.username.httpfrist_01.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class yibu1Activity extends AppCompatActivity {
private ListView mlistView;
    private static String Url="http://www.imooc.com/api/teacher?type=4&num=30";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yibu1);
        mlistView= (ListView) findViewById(R.id.lv_main);
        new NewsAsynTask().execute(Url);//将一个URL传递进去
//        ctrl+alt+t包围循环
//        Ctrl+j


    }
    /*
    * 将URL对应的Json格式数据转化为我们所封装的NewsBean对象
    * */
    private List<NewBean> getJsonData(String url){
        List<NewBean> newBeanList=new ArrayList<>();
        try {
            String Jsonstring=readStrem(new URL(url).openStream());
            //与url.openConnection().getInputStream()相同，根据URL直接联网获取网络数据，简单粗暴！返回值的类型为INputStream
           Log.d("xxs",Jsonstring);
            JSONObject jsonObject;
            NewBean newBean;
            try {
                jsonObject=new JSONObject(Jsonstring);
                JSONArray jsonArray=jsonObject.getJSONArray("data");
                for (int i=0;i<jsonArray.length();i++){
                    jsonObject=jsonArray.getJSONObject(i);
                    newBean=new NewBean();
                    newBean.newsIconUrl=jsonObject.getString("picSmall");
                    newBean.newsTitle=jsonObject.getString("name");
                    newBean.newsContent=jsonObject.getString("description");
                    newBeanList.add(newBean);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return newBeanList;
    }
    /*
    * 通过InputStream解析网页返回的数组
    * */
    private String readStrem(InputStream is){
        InputStreamReader isr;
        String result="";
        try {
            String line="";
            isr=new InputStreamReader(is,"utf-8");
            BufferedReader br=new BufferedReader(isr);
            while ((line=br.readLine())!=null){
                result+=line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
    * 实现网络的异步访问
    * */
    class NewsAsynTask extends AsyncTask<String,Void,List<NewBean>>{


        @Override
        protected List<NewBean> doInBackground(String... params) {

            return getJsonData(params[0]);
        }

        @Override
        protected void onPostExecute(List<NewBean> newBeens) {

            super.onPostExecute(newBeens);
            NewsAdapter adapter=new NewsAdapter(yibu1Activity.this,newBeens,mlistView);
            mlistView.setAdapter(adapter);
        }
    }
}
